package org.bandev.buddhaquotes.datastore.settings

import android.app.Activity
import android.content.Context
import android.util.Log
//import androidx.compose.ui.text.intl.Locale
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.bandev.buddhaquotes.settings.Settings
import java.util.Locale
import android.os.Build
import android.content.res.Configuration
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.savedstate.SavedStateRegistry


class SettingsViewModel(context: Context) : ViewModel() {

    private val repository = SettingsRepository(context.SettingsStoreCreator)
    val resources = context.resources // 使用传入的 context
    val settings = repository.settings.asLiveData()

    fun setTheme(theme: Settings.Theme) {
        viewModelScope.launch {
            repository.setTheme(theme)
            Log.d("SettingsViewModel", "Theme set to ${theme.name}")
        }
    }

    companion object {
        fun setLocales(language: Language) {
            var locale = when (language) {
                Language.ENGLISH -> Locale("en")
                Language.CHINESE -> Locale("zh")
            }
            Log.d("SettingsViewModel", "Locale set to ${locale.language}")
            Locale.setDefault(locale)
            val config = Configuration()
            config.setLocale(locale)
        }

        fun setLanguage (lang : String  ) {

            val locale = Locale(lang)
            Locale .setDefault(locale)
            val config = Configuration()
            config.setLocale(locale)
        }
    }

}

public enum class Language {
    ENGLISH,
    CHINESE,
}

